import * as sqlite_opt from "@/utils/sqliteUtil.js";

const thickListTableName = "t_thck_list";
const thickHistoryTableName = "t_thck_hist";

export const requireThickListData = (inMacType, inPltName, isPlan) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT DISTINCT `;
		sqlStr += `
			   plt_path,
			   mac_numb,
			   mac_name,
			   category,
			   mac_from,
			   mac_to,
			   lname,
			   plan_mac_numb,
		     
		      plt_name,
		      drw_pass,
		      mac_type,
		      st_name,
		      dmg_code		
		`
		sqlStr += `from ` + thickListTableName;
		sqlStr += ` where `;
		sqlStr += "   1 = 1 "
		sqlStr += inMacType ? (` AND mac_type = ` + inMacType) : " ";
		sqlStr += inPltName ? (` AND plt_name = '` + inPltName) + "' " : " ";
		sqlStr += isPlan ? (` AND (plan_mac_numb = 1 or plan_mac_numb = 2 or plan_mac_numb = 3)`) : " ";

		sqlStr += `  AND visible = 1  ORDER BY mac_numb`

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const requireThickPlantData = (inMacType) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT DISTINCT `;
		sqlStr += `
		       plt_name 
		`
		sqlStr += `from ` + thickListTableName + ` where visible = 1`

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const requireThickDetailData = (inDmgCode) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT * `;
		sqlStr += ` from ` + thickListTableName + ` where dmg_code = ` + inDmgCode
		sqlStr += `  ORDER BY pos_ordr`

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const requireThickDetailDataByPosNo = (inDmgCode, inPosNo) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT * `;
		sqlStr += ` from ` + thickListTableName + ` where dmg_code = ` + inDmgCode
		sqlStr += ` and pos_no = ` + inPosNo
		sqlStr += `  ORDER BY pos_ordr`

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}


export const requireThickHistoryData = (inDmgCode, inPosNo, inClsType, inPltName) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT * `;
		sqlStr += ` from ` + thickHistoryTableName + ` where 1 = 1`
		sqlStr += inDmgCode ? (` and  dmg_code = ` + inDmgCode) : '';
		sqlStr += inPosNo ? (` and  pos_no = ` + inPosNo) : '';
		sqlStr += inClsType ? (` and  cls_type = ` + inClsType) : '';
		sqlStr += inPltName ? (` and  plt_name = '` + inPltName + `'  `) : '';

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

/**
 * 获取计划实施状态 
 * 查询的状态数组   
 * 3：双同心圆  作业中
	1：单圆   未实施
	2：实心圆 已实施
	4：未计划
 */
export const requireThickImplementationData = (inStateArray, inMacType) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT DISTINCT dmg_code, plan_mac_numb `;
		sqlStr += ` from ` + thickListTableName + ` where 1 = 1 `

		sqlStr += ` and mac_type =  ` + inMacType;

		sqlStr += ` and  ( `

		for (let i = 0; i < inStateArray.length; i++) {
			if (i != 0)
				sqlStr += ` or `;
			sqlStr += ` plan_mac_numb = ` + inStateArray[i];

		}
		sqlStr += ` ) `;

		console.log("requireThickImplementationData  ---------------  sql:   ")
		console.log(sqlStr)

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const updateSurfaceTempByPosNo = (inDmgCode, inPosNo, inTemp) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `update ${thickListTableName} set temperature = "${inTemp}"  `;
		sqlStr += ` where 1 = 1 `
		sqlStr += ` and dmg_code =  ` + inDmgCode;
		sqlStr += ` and pos_no =  ` + inPosNo;


		console.log("updateSurfaceTempByPosNo  ---------------  sql:   ")
		console.log(sqlStr)

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const updateThickDetailDataByPosNoArray = (inDataArray) => {
	console.log("updateThickDetailDataByPosNoArray ----- start  -----");
	return new Promise((resolve, reject) => {

		let sqlArray = [];
		inDataArray.forEach(pObj => {
			// let keys = Object.keys(pObj)
			// let keyStr = keys.toString()
			let sqlStr = '';
			let valStr = ''
			// keys.forEach((item, index) => {
			// 	valStr += item + `  =  "`;
			// 	valStr += pObj[item] + `" `;

			// 	if (keys.length - 1 != index)
			// 		valStr += ","
			// })

			let updateDate = new Date().Format("yyyyMMdd HH:mm:ss")

			valStr += ` is_impossible = ${pObj.is_impossible} ,`;
			// if(pObj.isChanged) {
			valStr += pObj.is_impossible == 1 ? ` this_thck = "NA" , corrvalue="" , ` :
				` this_thck = "${pObj.this_thck}" ,corrvalue= "${pObj.corrvalue}" ,`;
			valStr += ` plan_cls_type = "${pObj.plan_cls_type}" ,`;
			console.log(valStr);
			valStr += ` isp_date = ` + (pObj.this_thck == "" ? ` "" ` : ` "${updateDate}" `);
			valStr += ` , `;
			console.log(valStr);
			valStr += ` dummy = "${pObj.dummy}" `;
			sqlStr += ` update ${thickListTableName} set ${valStr} where 1 =1 `;

			sqlStr += ` and  dmg_code = "${pObj.dmg_code}"  `;
			sqlStr += ` and  pos_no = "${pObj.pos_no}"  `;
			sqlStr += ` and  c_ordr = "${pObj.c_ordr}"  `;
			sqlStr += ` ;  `;

			console.log("updateThickDetailDataByPosNoArray  ---------------  sql:   ")
			console.log(sqlStr)
			sqlArray.push(sqlStr);
		})



		sqlite_opt.executeSql(sqlArray,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})

}

export const updateThickListPlanState = (inDmgCode, inState) => {
	console.log(`   updateThickListPlanState  ${inDmgCode}, ${inState} `)
	return new Promise((resolve, reject) => {		
		let tempSql = ` update ${thickListTableName} set plan_mac_numb = ${inState} where dmg_code = ${inDmgCode} `
		
		console.log(`   updateThickListPlanState  ${tempSql } `)
		
		sqlite_opt.executeSql(tempSql,
			(e) => {
				console.log("   updateThickListPlanState okokok");
				resolve(e);
			},
			(e) => {
				console.log("   updateThickListPlanState fail fail fail");
				console.log(e);
				reject(e);
			});
	})

}
